﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MyPS.Models;
using MyPSDALayer;
using Newtonsoft.Json;
using MyPS.CommonLib;
using Newtonsoft.Json.Converters;

namespace MyPS.Controllers
{
    public class AjaxRequestController : Controller
    {
        MyPSEntities db = new MyPSEntities();
        //
        // GET: /Ajax/

        public string GetArticleList(int pageIndex)
        {
            string type = Request["type"];
            IsoDateTimeConverter format = new IsoDateTimeConverter { DateTimeFormat = "yyyy/MM/dd HH:mm" };
            PageList<Article> page;
            if (string.IsNullOrEmpty(type))
            {
                page = new PageList<Article>(db.Articles.OrderByDescending(o => o.CreateDate), pageIndex, 10);
            }
            else
            {
                page = new PageList<Article>(db.Articles.Where(o => o.TypeName == type).OrderByDescending(o => o.CreateDate), pageIndex, 10);
            }

            return JsonConvert.SerializeObject(page, format);
        }

        public string GetCaseList(int pageIndex)
        {
            IsoDateTimeConverter format = new IsoDateTimeConverter { DateTimeFormat = "yyyy/MM/dd HH:mm" };
            string type = Request["type"];
            PageList<V_Case> page;
            if (string.IsNullOrEmpty(type))
            {
                page = new PageList<V_Case>(db.CaseBases.OrderByDescending(o => o.CreateDate).Select(o => new V_Case { ID = o.CaseID, Name = o.CaseName, CoverUrl = o.CoverUrl }), pageIndex, 6);
            }
            else
            {
                page = new PageList<V_Case>(db.CaseBases.Where(o => o.TypeName == type).OrderByDescending(o => o.CreateDate).Select(o => new V_Case { ID = o.CaseID, Name = o.CaseName, CoverUrl = o.CoverUrl }), pageIndex, 6);
            }

            return JsonConvert.SerializeObject(page, format);
        }

        public string GetTechList(int pageIndex)
        {
            string type = Request["type"];
            PageList<V_Tech> page;
            if (string.IsNullOrEmpty(type))
            {
                page = new PageList<V_Tech>(db.Teches.OrderByDescending(o => o.CreateDate).Select(o => new V_Tech { ID = o.TechID, Name = o.TechName, CoverUrl = o.CoverUrl }), pageIndex, 6);

            }
            else
            {
                page = new PageList<V_Tech>(db.Teches.Where(o => o.TypeName == type).OrderByDescending(o => o.CreateDate).Select(o => new V_Tech { ID = o.TechID, Name = o.TechName, CoverUrl = o.CoverUrl }), pageIndex, 6);
            }
            return JsonConvert.SerializeObject(page);
        }

        public string GetTeamList(int pageIndex)
        {
            PageList<V_Name_ID_Img> page = new PageList<V_Name_ID_Img>(db.TopTeams.OrderByDescending(o => o.CreateDate).Select(o => new V_Name_ID_Img { ID = o.ID, Name = o.TeamName, ImgUrl = o.CoverUrl }), pageIndex, 6);
            return JsonConvert.SerializeObject(page);
        }

        public string GetTaoBaoCaseList(int pageIndex)
        {
            PageList<V_Name_ID_Img> page = new PageList<V_Name_ID_Img>(db.CaseBases.Where(o => o.TypeName.Contains("淘宝装修")).OrderByDescending(o => o.CreateDate).Select(o => new V_Name_ID_Img { ID = o.CaseID, Name = o.CaseName, ImgUrl = o.CoverUrl }), pageIndex, 6);
            return JsonConvert.SerializeObject(page);
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }

    }
}
